home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 11 / AMUG BBS in a Box Volume XI (April 1994) (MacWizards).iso / Files / Prog / M / Mini.sit / mini / dialer.c / dialer.c
Encoding:
C/C++ Source or Header  |  1987-09-16  |  10.2 KB  |  368 lines  |  [TEXT/MPS ]

  1. /***********************************************************************
  2. *
  3. *    A module for maintaining a phone book
  4. *
  5. *    Jerry LeVan
  6. *    325 Boone Trail
  7. *    Richmond Ky 40475
  8. *
  9. *    New File: Sept 14,1987
  10. *
  11. **********************************************************************/
  12.  
  13. # include <types.h>                 /* Nearly always required */
  14. # include <quickdraw.h>             /* To access the qd globals */
  15. # include <fonts.h>                 /* Only for InitFonts() trap */
  16. # include <Lists.h>
  17. # include <Textedit.h>
  18. # include <Windows.h>
  19. # include <Events.h>
  20. # include <Menus.h>
  21. # include <Dialogs.h>
  22. # include <segload.h>                /* UnloadSeg() */
  23. # include <resources.h>
  24. # include <OSUtils.h>
  25.  
  26. /* Define Item numbers and dialog id */
  27. #define MYDIALOG 200
  28.  
  29. #define NAME 7
  30. #define NUMBER 8
  31. #define USER 9
  32. #define CANCEL 2
  33. #define DIAL 1
  34. #define ADD 3
  35. #define DELETE 4
  36. #define CNUMBER 11
  37.  
  38. /* key codes */
  39. #define CR 13
  40. #define ENTER 3
  41.  
  42. /* max characters in buffers */
  43. #define MAXCHARS 80
  44.  
  45.  
  46. #define __SEG__ Dialer
  47. /**********************************************************************/
  48. static ListHandle InitList(theWindow)
  49.  DialogPtr theWindow;
  50. { Cell theCell;
  51.   short i;
  52.   short tmp;
  53.   char name[MAXCHARS],*p;
  54.   short rescnt,id,whichRow;
  55.   FontInfo finfo;
  56.   Handle aHandle;
  57.   ResType aType;
  58.   Rect box,listRect,theRect;
  59.   ListHandle theList;
  60.   
  61.   /* get the user item box */
  62.    GetDItem(theWindow,USER,&aType,&aHandle,&box);
  63.    
  64.   /* set the view rectangle so that we have a snug fit */
  65.   GetFontInfo(&finfo);
  66.   tmp = finfo.ascent+finfo.descent+finfo.leading;
  67.   theRect.top = box.top;
  68.   theRect.left = box.left;
  69.   theRect.bottom = box.top + ((box.bottom-box.top)/tmp)*tmp; 
  70.   theRect.right = box.right-20; /* 20 is room for scroll bar */
  71.   
  72.   /* set cell size */
  73.   theCell.v=0;                /* take default height */
  74.   theCell.h=theRect.right-theRect.left;    /* and use rect as width */ 
  75.   
  76.   /* Set the list size */
  77.   SetRect(&listRect,0,0,0,0);    /* empty list */
  78.   
  79.   /* create the list */
  80.   theList = LNew(&theRect,    /* Display rectangle */
  81.            &listRect,    /* Initial size of list */
  82.          &theCell,    /* Cell size */
  83.          0,        /* Use standard list proc */
  84.          theWindow,    /* window to draw in */
  85.          0,        /* drawing off */
  86.          0,        /* no grow box */
  87.          0,        /* has no horizontal scroll bar */
  88.          1);    /* has vertical scroll bar */
  89.      
  90.   /* Load the cells */
  91.   
  92.   /* step one... count the names */
  93.   rescnt = CountResources('DIAL');
  94.   
  95.   /* step two...Add The Cells */
  96.   LAddColumn(1,0,theList);    /* add the first column */
  97.   
  98.   /* Step three...Add the contents */
  99.   whichRow = 0;
  100.   
  101.   for(i=0;i<rescnt;i++){
  102.     
  103.     SetResLoad(0);  
  104.     aHandle = GetIndResource('DIAL',i+1);
  105.     GetResInfo(aHandle,&id,&aType,name);
  106.     ReleaseResource(aHandle);
  107.     SetResLoad(1);
  108.     
  109.     tmp = 0; p = (char *)name;
  110.     while(*p++)tmp++;
  111.     
  112.     if (tmp){
  113.         theCell.v = whichRow++;    /* row    */
  114.         theCell.h = 0;    /* first column */
  115.         LAddRow(1,rescnt+1,theList); /* guarantee addition at end of list */
  116.         LSetCell(name,tmp,&theCell,theList); /* set the value */
  117.      }
  118.    }
  119.     (*theList)->selFlags = lOnlyOne;    /* only one selection at a time */
  120.     return theList;
  121.  }
  122. /**********************************************************************/
  123. /* this is the "userItem" to be installed in the dialog */
  124.  
  125. static pascal void DoUpDate(theWindow,item)
  126.  DialogPtr theWindow;
  127.  short item;
  128.  { short atype,tmp;
  129.    Handle ahandle;
  130.    Rect box,theRect;
  131.    FontInfo finfo;
  132.    
  133.    
  134.    GetDItem(theWindow,item,&atype,&ahandle,&box);
  135.    GetFontInfo(&finfo);
  136.    
  137.    tmp = finfo.ascent+finfo.descent+finfo.leading;
  138.    theRect.top = box.top;
  139.    theRect.left = box.left;
  140.    theRect.bottom = box.top + ((box.bottom-box.top)/tmp)*tmp; 
  141.    theRect.right = box.right-20; /* 20 is room for scroll bar */
  142.    
  143.    InsetRect(&theRect,-1,-1);    /* shrink for a nice fit */
  144.    FrameRect(&theRect);            /* redraw the frame */
  145.    LUpdate(theWindow->visRgn,(ListHandle)((WindowPeek)theWindow)->refCon);   
  146.    }
  147. /**********************************************************************/ 
  148. /* this procedure is called by ModalDialog */
  149.  
  150. static pascal char myFilter(theWindow,event,item)
  151.     DialogPtr theWindow;
  152.     EventRecord *event;
  153.     short *item;
  154.     
  155.  {  Point theLoc;
  156.     char result,buffer[MAXCHARS];
  157.     Cell tmpCell,theCell;
  158.     Rect box;
  159.     short atype,dataLen;
  160.     Handle ahandle,numHandle;
  161.     ListHandle theList;
  162.     
  163.     /* recover a handle to the list */
  164.     theList = (ListHandle)((WindowPeek)theWindow)->refCon;
  165.     
  166.     result = 0;    /* assume we don't want to handle */
  167.     
  168.      switch (event->what) {
  169.         case mouseDown:
  170.              {  
  171.             theLoc = event->where;
  172.             GetDItem(theWindow,USER,&atype,&ahandle,&box);
  173.             GlobalToLocal(&theLoc);
  174.             
  175.             /* check if mousedown in user item */
  176.             if (PtInRect(&theLoc,&box))
  177.              {
  178.               if(LClick(&theLoc,event->modifiers,theList)){
  179.                    *item = DIAL; /* got a double click */
  180.                    result = 1;
  181.              } /* end of double click processing */           
  182.             else { /* refresh the current number field */
  183.                 SetRect(&theCell,0,0,0,0);
  184.                 if(LGetSelect(true,&theCell,theList)){
  185.                      /* get the selection */
  186.                      dataLen = MAXCHARS; /* max length */
  187.                      LGetCell(buffer,&dataLen,&theCell,theList);
  188.                      buffer[dataLen] = 0; /* make into c-string */
  189.                      numHandle=GetNamedResource('DIAL',buffer);
  190.                      HLock(numHandle);    /* lock it */
  191.                         
  192.                      GetDItem(theWindow,CNUMBER,&atype,&ahandle,&box);
  193.                      SetIText(ahandle,*numHandle);
  194.                      
  195.                      HUnlock(numHandle);
  196.                      ReleaseResource(numHandle);
  197.                 } else{ /* erase the number */
  198.                     GetDItem(theWindow,CNUMBER,&atype,&ahandle,&box);
  199.                     SetIText(ahandle,"");
  200.                    }
  201.                } /* if point in rect */
  202.             }     /* click */
  203.           } /* mouse down */
  204.             break;
  205.         case keyDown:
  206.         case autoKey: /* fake a dial button hit */
  207.                 if(((char)event->message ==CR) || ((char)event->message==ENTER))
  208.                 {*item = DIAL; result =1;};
  209.             break;
  210.         break;
  211.         }
  212.      return(result);
  213. }
  214. /**********************************************************************/
  215. PhoneManager(refnum)
  216.   short refnum;
  217. {
  218.   Rect Box,theRect;
  219.   short item ;
  220.   short type;
  221.   Handle theHandle,numHandle,ahandle;
  222.   Cell theCell,tmpCell;
  223.   char buffer[MAXCHARS],numbuff[MAXCHARS],*p;
  224.   short dataLen,id,tmp;
  225.   long two,one,tmplong;
  226.   GrafPtr     savePort;
  227.   DialogPtr theWindow;
  228.   ListHandle theList;
  229.   
  230.     GetPort(&savePort);
  231.  
  232.    /* get the dialog (created invisible) */
  233.    theWindow = (DialogPtr)GetNewDialog(MYDIALOG,0,(Ptr)-1);
  234.    SetPort(theWindow);
  235.    
  236.    /* build the list and store the list handle so the filter proc can find it */
  237.    theList = InitList(theWindow);
  238.    ((WindowPeek)theWindow)->refCon = (long)theList;
  239.    
  240.    /* set the frame draw procedure */
  241.    GetDItem(theWindow,USER,&type,&theHandle,&Box);
  242.    SetDItem(theWindow,USER, type,DoUpDate,&Box);
  243.    
  244.    
  245.    /* outline the dial button */
  246.    GetDItem(theWindow,DIAL,&type,&theHandle,&Box);
  247.    PenSize(3,3);
  248.    InsetRect(&Box,-4,-4);
  249.    FrameRoundRect(&Box,16,16);
  250.    PenSize(1,1);
  251.    
  252.    /* select the add entry fields */
  253.     SelIText(theWindow,NAME,0,32767);
  254.    
  255.    /* turn on list drawing */
  256.    LDoDraw(1,theList);
  257.    
  258.    /* show the dialog window  */
  259.    ShowWindow(theWindow);
  260.    
  261.    /* hang around until <cr> or Dial  or cancel hit */
  262.    do{ 
  263.        ModalDialog(myFilter,&item);
  264.     switch (item){
  265.       case DIAL: /* dial the number if there is a selection */
  266.               SetRect(&theCell,0,0,0,0);
  267.               if(LGetSelect(true,&theCell,theList)){
  268.              /* get the selection */
  269.              dataLen = MAXCHARS; /* max length */
  270.              LGetCell(buffer,&dataLen,&theCell,theList);
  271.              buffer[dataLen] = 0; /* make into c-string */
  272.              numHandle=GetNamedResource('DIAL',buffer);
  273.              HLock(numHandle);    /* lock it */
  274.              
  275.              /* use Hayes AT commands */
  276.              two = 2;
  277.              FSWrite(refnum,&two,"AT"); Delay(20,&tmplong);
  278.              two =2;
  279.              FSWrite(refnum,&two,"DT"); Delay(20,&tmplong);
  280.              /* and now the number  */
  281.              tmplong = 0; p = *numHandle;
  282.              while(*p++)tmplong++;  /* compute length */
  283.             FSWrite(refnum,&tmplong,*numHandle);
  284.             one = 1;
  285.             FSWrite(refnum,&one,"\n");
  286.             HUnlock(numHandle);
  287.             ReleaseResource(numHandle);
  288.              
  289.              break;
  290.             } else { SysBeep(10); item = 0;} /* nothing was selected */
  291.         case CANCEL: break; /* leave the dialog */
  292.         case DELETE:
  293.               SetRect(&theCell,0,0,0,0);
  294.               if(LGetSelect(true,&theCell,theList)){
  295.              /* get the selection */
  296.              dataLen = MAXCHARS; /* max length */
  297.              LGetCell(buffer,&dataLen,&theCell,theList);
  298.              buffer[dataLen] = 0; /* make into c-string */
  299.              numHandle=GetNamedResource('DIAL',buffer);
  300.              RmveResource(numHandle);
  301.              DisposHandle(numHandle);
  302.              /* now delete the cell and its contents */
  303.              LDelRow(1,theCell.v,theList);
  304.              /* update the current number field */
  305.              GetDItem(theWindow,CNUMBER,&type,&ahandle,&Box);
  306.              SetIText(ahandle,"");
  307.             } else SysBeep(10);  /* no selection */
  308.              break;
  309.         case ADD:
  310.             do 
  311.                  id = UniqueID('DIAL');
  312.             while (id <= 127);  /* get a new id */
  313.             
  314.             GetDItem(theWindow,NAME,&type,&ahandle,&Box); /* get the name */
  315.             GetIText(ahandle,buffer);
  316.             
  317.             /* if name in use, then ignore request  */
  318.             theHandle = GetNamedResource('DIAL',buffer);
  319.             
  320.             if(theHandle == nil){ /* no resource ,ok to add */
  321.             
  322.                 GetDItem(theWindow,NUMBER,&type,&ahandle,&Box);
  323.                 GetIText(ahandle,numbuff); /* get the text */
  324.                 
  325.                 tmp = 0; p = (char *)numbuff; /* get length of number */
  326.                 while(*p++)tmp++;
  327.                 
  328.                 tmp++;  /* for the null at the end of the string */
  329.                 
  330.                 PtrToHand(numbuff,&ahandle,(long)tmp);
  331.                 
  332.                 AddResource(ahandle,'DIAL',id,buffer);
  333.                 WriteResource(ahandle);    /* write it */
  334.                 ReleaseResource(ahandle) ; /* free it */
  335.                 
  336.                 /* add to the list */
  337.                 theCell.v = (**theList).dataBounds.bottom;
  338.                 theCell.h = 0;
  339.                 LAddRow(1,theCell.v,theList);
  340.                 
  341.                 tmp = 0; p = (char *)buffer; /* get length of name */
  342.                 while(*p++)tmp++;
  343.                 
  344.                 LSetCell(buffer,tmp,&theCell,theList);
  345.              }
  346.              if(theHandle)SysBeep(10);  /* signal failure */
  347.             /* reset the fields */
  348.             GetDItem(theWindow,NAME,&type,&ahandle,&Box); /* get the name field */
  349.             SetIText(ahandle,"Put Name Here");            
  350.             SelIText(theWindow,NAME,0,32767);
  351.             /* and now the number field */
  352.             GetDItem(theWindow,NUMBER,&type,&ahandle,&Box); /* get the number field */
  353.             SetIText(ahandle,"Put Number Here");            
  354.             break;
  355.             
  356.         default: break;
  357.      } /* switch */
  358.     }          
  359.    while((item != CANCEL) && (item != DIAL));
  360.    
  361.    /* get rid of data structures and return to finder */
  362.    LDispose(theList);
  363.    DisposDialog(theWindow);
  364.    SetPort(savePort);
  365.    
  366. }
  367. /**********************************************************************/
  368.